import java.net.URL;

import org.w3c.dom.NodeList;


public class FollowingFetcher {
	Client client;
	public FollowingFetcher(Client client) {
		this.client = client;
	}
	
	public void fetch() {
		URL url = client.getFollowingURL();
		Downloader downloader = new Downloader(url);
		String html = downloader.fetchAsString();
		String followers = parse(html);
		client.handleFollowingResults(followers);
	}
	
	private String parse(String string) {
		if(string == null) {
			System.out.println(" + Fetched followings (0)");
			return "";
		}
		
		NodeList ids = Helper.parseXMLWithXPath(string, "//id/text()");
		String results = "";
		for(int i = 0; i < ids.getLength(); i++) {
			results += ids.item(i).getTextContent() + ",";
		}

		System.out.println(" + Fetched followings ("+ ids.getLength() +")");
		return results;
	}
}
